home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / term_utils.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  4.2 KB  |  201 lines

  1. from rdflib import *
  2. from rdflib.Graph import QuotedGraph, Graph, ConjunctiveGraph, BackwardCompatGraph
  3.  
  4. #Takes an instance of a Graph (Graph, QuotedGraph, ConjunctiveGraph, or BackwardCompatGraph)
  5. #and returns the Graphs identifier and 'type' ('U' for Graphs, 'F' for QuotedGraphs ).
  6. def normalizeGraph(graph):
  7.     if isinstance(graph,QuotedGraph):
  8.         return graph.identifier, 'F'
  9.     else:
  10.         return graph.identifier , term2Letter(graph.identifier)
  11.  
  12. TERM_INSTANCIATION_DICT ={
  13.     'U':URIRef,
  14.     'B':BNode,
  15.     'V':Variable,
  16.     'L':Literal
  17. }
  18.  
  19. GRAPH_TERM_DICT = {
  20.     'F': (QuotedGraph, URIRef),
  21.     'U': (Graph, URIRef),
  22.     'B': (Graph, BNode)
  23. }
  24.  
  25. def term2Letter(term):
  26.     if isinstance(term,URIRef):
  27.         return 'U'
  28.     elif isinstance(term,BNode):
  29.         return 'B'
  30.     elif isinstance(term,Literal):
  31.         return 'L'
  32.     elif isinstance(term,QuotedGraph):
  33.         return 'F'
  34.     elif isinstance(term,Variable):
  35.         return 'V'
  36.     elif isinstance(term,Graph):
  37.         return term2Letter(term.identifier)
  38.     elif term is None:
  39.         return 'L'
  40.     else:
  41.         raise Exception("The given term (%s) is not an instance of any of the known types (URIRef,BNode,Literal,QuotedGraph, or Variable).  It is a %s"%(term,type(term)))
  42.  
  43. def constructGraph(term):
  44.     return GRAPH_TERM_DICT[term]
  45.  
  46. def triplePattern2termCombinations((s,p,o)):
  47.     combinations=[]
  48.     #combinations.update(TERM_COMBINATIONS)
  49.     if isinstance(o,Literal):
  50.         for key,val in TERM_COMBINATIONS.items():
  51.             if key[OBJECT] == 'O':
  52.                 combinations.append(val)
  53.     return combinations
  54.  
  55. def type2TermCombination(member,klass,context):
  56.     try:
  57.         rt = TERM_COMBINATIONS['%sU%s%s'%(term2Letter(member),term2Letter(klass),normalizeGraph(context)[-1])]
  58.         return rt
  59.     except:
  60.         raise Exception("Unable to persist classification triple: %s %s %s"%(member,'rdf:type',klass,context))
  61.  
  62. def statement2TermCombination(subject,predicate,obj,context):
  63.     return TERM_COMBINATIONS['%s%s%s%s'%(term2Letter(subject),term2Letter(predicate),term2Letter(obj),normalizeGraph(context)[-1])]
  64.  
  65. SUBJECT = 0
  66. PREDICATE = 1
  67. OBJECT = 2
  68. CONTEXT = 3
  69.  
  70. TERM_COMBINATIONS = {
  71.     'UUUU' : 0,
  72.     'UUUB' : 1,
  73.     'UUUF' : 2,
  74.     'UUVU' : 3,
  75.     'UUVB' : 4,
  76.     'UUVF' : 5,
  77.     'UUBU' : 6,
  78.     'UUBB' : 7,
  79.     'UUBF' : 8,
  80.     'UULU' : 9,
  81.     'UULB' : 10,
  82.     'UULF' : 11,
  83.     'UUFU' : 12,
  84.     'UUFB' : 13,
  85.     'UUFF' : 14,
  86.  
  87.     'UVUU' : 15,
  88.     'UVUB' : 16,
  89.     'UVUF' : 17,
  90.     'UVVU' : 18,
  91.     'UVVB' : 19,
  92.     'UVVF' : 20,
  93.     'UVBU' : 21,
  94.     'UVBB' : 22,
  95.     'UVBF' : 23,
  96.     'UVLU' : 24,
  97.     'UVLB' : 25,
  98.     'UVLF' : 26,
  99.     'UVFU' : 27,
  100.     'UVFB' : 28,
  101.     'UVFF' : 29,
  102.  
  103.     'VUUU' : 30,
  104.     'VUUB' : 31,
  105.     'VUUF' : 33,
  106.     'VUVU' : 34,
  107.     'VUVB' : 35,
  108.     'VUVF' : 36,
  109.     'VUBU' : 37,
  110.     'VUBB' : 38,
  111.     'VUBF' : 39,
  112.     'VULU' : 40,
  113.     'VULB' : 41,
  114.     'VULF' : 42,
  115.     'VUFU' : 43,
  116.     'VUFB' : 44,
  117.     'VUFF' : 45,
  118.  
  119.     'VVUU' : 46,
  120.     'VVUB' : 47,
  121.     'VVUF' : 48,
  122.     'VVVU' : 49,
  123.     'VVVB' : 50,
  124.     'VVVF' : 51,
  125.     'VVBU' : 52,
  126.     'VVBB' : 53,
  127.     'VVBF' : 54,
  128.     'VVLU' : 55,
  129.     'VVLB' : 56,
  130.     'VVLF' : 57,
  131.     'VVFU' : 58,
  132.     'VVFB' : 59,
  133.     'VVFF' : 60,
  134.  
  135.     'BUUU' : 61,
  136.     'BUUB' : 62,
  137.     'BUUF' : 63,
  138.     'BUVU' : 64,
  139.     'BUVB' : 65,
  140.     'BUVF' : 66,
  141.     'BUBU' : 67,
  142.     'BUBB' : 68,
  143.     'BUBF' : 69,
  144.     'BULU' : 70,
  145.     'BULB' : 71,
  146.     'BULF' : 72,
  147.     'BUFU' : 73,
  148.     'BUFB' : 74,
  149.     'BUFF' : 75,
  150.  
  151.     'BVUU' : 76,
  152.     'BVUB' : 77,
  153.     'BVUF' : 78,
  154.     'BVVU' : 79,
  155.     'BVVB' : 80,
  156.     'BVVF' : 81,
  157.     'BVBU' : 82,
  158.     'BVBB' : 83,
  159.     'BVBF' : 84,
  160.     'BVLU' : 85,
  161.     'BVLB' : 86,
  162.     'BVLF' : 87,
  163.     'BVFU' : 88,
  164.     'BVFB' : 89,
  165.     'BVFF' : 90,
  166.  
  167.     'FUUU' : 91,
  168.     'FUUB' : 92,
  169.     'FUUF' : 93,
  170.     'FUVU' : 94,
  171.     'FUVB' : 95,
  172.     'FUVF' : 96,
  173.     'FUBU' : 97,
  174.     'FUBB' : 98,
  175.     'FUBF' : 99,
  176.     'FULU' : 100,
  177.     'FULB' : 101,
  178.     'FULF' : 102,
  179.     'FUFU' : 103,
  180.     'FUFB' : 104,
  181.     'FUFF' : 105,
  182.  
  183.     'FVUU' : 106,
  184.     'FVUB' : 107,
  185.     'FVUF' : 108,
  186.     'FVVU' : 109,
  187.     'FVVB' : 110,
  188.     'FVVF' : 111,
  189.     'FVBU' : 112,
  190.     'FVBB' : 113,
  191.     'FVBF' : 114,
  192.     'FVLU' : 115,
  193.     'FVLB' : 116,
  194.     'FVLF' : 117,
  195.     'FVFU' : 118,
  196.     'FVFB' : 119,
  197.     'FVFF' : 120,
  198. }
  199.  
  200. REVERSE_TERM_COMBINATIONS = dict([(value,key) for key,value in TERM_COMBINATIONS.items()])
  201.